package com.zlm.user.emp.service.impl;

import com.zlm.common.base.AbstractBaseService;
import com.zlm.common.base.PageEntity;
import com.zlm.common.constants.CommonConstants;
import com.zlm.common.constants.NumberConstants;
import com.zlm.common.dto.userEmp.OrgParamsDto;
import com.zlm.common.dto.userEmp.SysOrgDto;
import com.zlm.common.entity.SysOrgDO;
import com.zlm.common.enums.CompanyNatureEnum;
import com.zlm.common.enums.CompanyScaleEnum;
import com.zlm.user.emp.dao.EmpOrgCollectDao;
import com.zlm.user.emp.dao.SysOrgDao;
import com.zlm.user.emp.service.SysOrgService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: duyufu
 * @Date: 2019/4/28 14:35
 */
@Service
public class SysOrgServiceImpl extends AbstractBaseService<SysOrgDao, SysOrgDO> implements SysOrgService {

    @Autowired
    @Override
    public void setBaseDao(SysOrgDao sysOrgDao) {
        this.baseDao = sysOrgDao;
    }
    @Autowired
    private EmpOrgCollectDao empOrgCollectDao;

    @Override
    public SysOrgDto getSysOrg(Integer empId,Integer orgId) {
        SysOrgDto sysOrg = baseDao.getSysOrg(orgId);
        sysOrg.setOrgNatureName(CompanyNatureEnum.getNameByCode(sysOrg.getOrgNature()));
        sysOrg.setOrgScaleName(CompanyScaleEnum.getNameByCode(sysOrg.getOrgScale()));
        if (StringUtils.isNotBlank(sysOrg.getAttUrl())) {
            sysOrg.setAttUrl(CommonConstants.IMG_URL.concat(sysOrg.getAttUrl()));
        }
        List<String> orgAttApplyRealList =  baseDao.queryOrgAttApplyRealList(sysOrg.getOrgId());
        List<String> tempList = new ArrayList<>();
        if (orgAttApplyRealList.size()==1&&null==orgAttApplyRealList.get(0)||orgAttApplyRealList.size()==0) {
            //查询列表为空

        }else {
            //查询列表不为空
            for (String orgAttApplyReal : orgAttApplyRealList) {
                String orgReal = CommonConstants.IMG_URL.concat(orgAttApplyReal);
                tempList.add(orgReal);
            }
        }
        orgAttApplyRealList = tempList;
        tempList = null;
        sysOrg.setOrgAttApplyRealList(orgAttApplyRealList);
        if (null != empId) {
            int countOrg = empOrgCollectDao.countEmpOrgCollect(orgId,empId);
            if (countOrg > 0) {
                sysOrg.setIsCollect(1);
            }else {
                sysOrg.setIsCollect(0);
            }
        }else {
            sysOrg.setIsCollect(0);
        }

        return sysOrg;
    }

    @Override
    public PageEntity queryStarEnterprise(OrgParamsDto orgParamsDto) {
        int count = baseDao.countStarEnterprise(orgParamsDto);
        int pageNumber,pageSize;
        if (null == orgParamsDto.getPageSize()|| orgParamsDto.getPageSize()>= CommonConstants.maxPageSize) {
            pageSize = CommonConstants.pageSize;
        }else {
            pageSize = orgParamsDto.getPageSize();
        }
        if (null == orgParamsDto.getPageNumber() || orgParamsDto.getPageNumber().intValue() == NumberConstants.NUM_0) {
            pageNumber = CommonConstants.pageNumber;
        }else {
            pageNumber = (orgParamsDto.getPageNumber()-1) * pageSize;
        }
        orgParamsDto.setPageSize(pageSize);
        orgParamsDto.setPageNumber(pageNumber);
        if (null != orgParamsDto.getOrgNature() && orgParamsDto.getOrgNature().equals(CompanyNatureEnum.COMPANY_NATURE_NO_LIMIT.getCode())) {
            orgParamsDto.setOrgNature(null);
        }
        if (null != orgParamsDto.getOrgScale() && orgParamsDto.getOrgScale().equals(CompanyScaleEnum.COMPANY_SCALE_PEOPLE_NO_LIMIT.getCode())) {
            orgParamsDto.setOrgScale(null);
        }
        List<SysOrgDto> orgList = baseDao.queryStarEnterprise(orgParamsDto);
        for (SysOrgDto sysOrgDto : orgList) {
            sysOrgDto.setOrgNatureName(CompanyNatureEnum.getNameByCode(sysOrgDto.getOrgNature()));
            sysOrgDto.setOrgScaleName(CompanyScaleEnum.getNameByCode(sysOrgDto.getOrgScale()));
            if (StringUtils.isNotBlank(sysOrgDto.getAttUrl())) {
                sysOrgDto.setAttUrl(CommonConstants.IMG_URL.concat(sysOrgDto.getAttUrl()));
            }
        }
        PageEntity page = getPage(orgParamsDto.getPageNumber(),orgParamsDto.getPageSize(),count,orgList);
        return page;
    }
}
